package com.org.controller;

import com.org.dao.UserMapper;
import com.org.pojo.User;
import com.org.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService service;

    @RequestMapping(value = "getUser",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getUser(){
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","success");
        map.put("list",service.getUser());
        return map;
    }

    @RequestMapping(value = "/insertUser",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> insertUser(User user){
        Map<String,Object> map = new HashMap<>();
        if (service.insertUser(user)){
            map.put("code",200);
            map.put("msg","success");
        }
        else {
            map.put("code",400);
        }
        return map;
    }

    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updatePwd(@Param(value = "id") int id,@Param(value = "pwd") String pwd){
        Map<String,Object> map = new HashMap<>();
        if (service.updatePwd(id,pwd)){
            map.put("code",200);
            map.put("msg","success");
        }
        else {
            map.put("code",400);
        }
        return map;
    }

    @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> deleteById(@Param(value = "id") int id){
        Map<String,Object> map = new HashMap<>();
        if (service.deleteById(id)){
            map.put("code",200);
            map.put("msg","success");
        }
        else {
            map.put("code",400);
        }
        return map;
    }
}
